package com.mingqijia.gassafety.webserver.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.shared.request.VccAccountNumberListRequest;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.shared.vcc.VccSystemProvider;
import com.mingqijia.gassafety.webserver.request.AccountNumberAddRequest;
import com.mingqijia.gassafety.webserver.request.AccountNumberEditRequest;
import com.mingqijia.gassafety.webserver.request.AccountNumberIdRequest;
import com.mingqijia.gassafety.webserver.request.AccountNumberIdsRequest;
import com.mingqijia.gassafety.webserver.request.AccountNumberListRequest;
import com.mingqijia.gassafety.webserver.response.*;
import com.mingqijia.gassafety.webserver.service.AccountNumberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: gassafety
 * @description: 账号管理实现
 * @author: zm
 * @create: 2024-07-30 13:43
 **/
@Service
@Slf4j
public class AccountNumberServiceImpl implements AccountNumberService {

    @Value(value = "${gassafety.ewt.url}")
    public String targetUrl;

    @Autowired
    private VccSystemProvider vccSystemProvider;

    @Override
    public CommonResponse<PageUtil<AccountNumberResp>> getList(AccountNumberListRequest req) {
        String spId = ApplicationUtils.getWorkingSpId();
        String userId = ApplicationUtils.getUserId();

        VccAccountNumberListRequest vccReq = new VccAccountNumberListRequest();
        BeanUtils.copyProperties(req, vccReq);
        CommonResponse<String> body = vccSystemProvider.account_number_list(vccReq, spId, userId, targetUrl);
        log.info("组织部门列表返回结果：{}", body);
        AccountNumberListResp resp = JSONArray.parseObject(body.getData(), AccountNumberListResp.class);
        PageUtil<AccountNumberResp> pageUtil = new PageUtil<>();
        if (resp != null) {
            pageUtil = new PageUtil<>(req.getPageIndex(), req.getPageSize(), resp.getTotalSize(), resp.getDatas());
        }
        return excuteBody(body, pageUtil);
    }

    @Override
    public CommonResponse<AccountNumberResp> detail(AccountNumberIdRequest req) {
        String spId = ApplicationUtils.getWorkingSpId();
        String userId = ApplicationUtils.getUserId();
        CommonResponse<String> body = vccSystemProvider.account_number_detail(req.getId(), spId, userId, targetUrl);
        log.info("账号详情 返回结果：{}", body);
        AccountNumberResp resp = new AccountNumberResp();
        if (null != body && null != body.getData()) {
            resp = JSONObject.parseObject(body.getData(), AccountNumberResp.class);
        }
        return excuteBody(body, resp);
    }

    @Override
    public CommonResponse<Boolean> add(AccountNumberAddRequest req) {
        String spId = ApplicationUtils.getWorkingSpId();
        String userId = ApplicationUtils.getUserId();

        VccAccountNumberListRequest vccReq = new VccAccountNumberListRequest();
        BeanUtils.copyProperties(req, vccReq);
        CommonResponse<String> body = vccSystemProvider.account_number_add(vccReq, spId, userId, targetUrl);
        log.info("账号新增 返回结果：{}", body);
        return excuteBody(body, true);
    }

    public <T> CommonResponse<T> excuteBody(CommonResponse<String> body, T data) {
        if (null != body && null != body.getCode() && body.getCode() != 0) {
            return CommonResponse.failure(body.getCode(), body.getMsg());
        }
        return CommonResponse.success(data);
    }

    @Override
    public CommonResponse<Boolean> edit(AccountNumberEditRequest req) {
        String spId = ApplicationUtils.getWorkingSpId();
        String userId = ApplicationUtils.getUserId();

        VccAccountNumberListRequest vccReq = new VccAccountNumberListRequest();
        BeanUtils.copyProperties(req, vccReq);
        CommonResponse<String> body = vccSystemProvider.account_number_edit(vccReq, req.getId(), spId, userId, targetUrl);
        log.info("账号编辑 返回结果：{}", body);
        return excuteBody(body, true);
    }

    @Override
    public CommonResponse<Boolean> activate(AccountNumberIdsRequest req) {
        String spId = ApplicationUtils.getWorkingSpId();
        String userId = ApplicationUtils.getUserId();
        if (CollectionUtils.isEmpty(req.getIds())) {
            return CommonResponse.failure("请选择账号进行激活！");
        }
        CommonResponse<String> body = new CommonResponse<>();
        String state = "A";
        if (req.getIds().size() - 1 == 0) {
            body = vccSystemProvider.account_number_update_status(req.getIds().get(0), state, spId, userId, targetUrl);
        } else {
            body = vccSystemProvider.account_number_batch_update_status(req.getIds(), state, spId, userId, targetUrl);
        }
        log.info("激活 返回结果：{}", body);
        return excuteBody(body, true);
    }

    @Override
    public CommonResponse<Boolean> lock(AccountNumberIdsRequest req) {
        String spId = ApplicationUtils.getWorkingSpId();
        String userId = ApplicationUtils.getUserId();
        if (CollectionUtils.isEmpty(req.getIds())) {
            return CommonResponse.failure("请选择账号进行锁定！");
        }
        CommonResponse<String> body = new CommonResponse<>();
        String state = "L";
        if (req.getIds().size() - 1 == 0) {
            body = vccSystemProvider.account_number_update_status(req.getIds().get(0), state, spId, userId, targetUrl);
        } else {
            body = vccSystemProvider.account_number_batch_update_status(req.getIds(), state, spId, userId, targetUrl);
        }
        log.info("锁定 返回结果：{}", body);
        return excuteBody(body, true);
    }

    @Override
    public CommonResponse<Boolean> delete(AccountNumberIdsRequest req) {
        String spId = ApplicationUtils.getWorkingSpId();
        String userId = ApplicationUtils.getUserId();
        if (CollectionUtils.isEmpty(req.getIds())) {
            return CommonResponse.failure("请选择账号进行删除");
        }
        CommonResponse<String> body = new CommonResponse<>();
        String state = "X";
        if (req.getIds().size() - 1 == 0) {
            body = vccSystemProvider.account_number_update_status(req.getIds().get(0), state, spId, userId, targetUrl);
        } else {
            body = vccSystemProvider.account_number_batch_update_status(req.getIds(), state, spId, userId, targetUrl);
        }
        log.info("删除 返回结果：{}", body);
        return excuteBody(body, true);
    }

    @Override
    public CommonResponse<Boolean> reset(AccountNumberIdRequest req) {
        String spId = ApplicationUtils.getWorkingSpId();
        String userId = ApplicationUtils.getUserId();

        CommonResponse<String> body = vccSystemProvider.account_number_reset_password(req.getState(), req.getId(), spId, userId, targetUrl);
        log.info("重置密码 返回结果：{}", body);
        return excuteBody(body, true);
    }

    @Override
    public CommonResponse<List<AccountNumberRoleListResp>> roleList() {
        String spId = ApplicationUtils.getWorkingSpId();
        String userId = ApplicationUtils.getUserId();
        CommonResponse<String> body = vccSystemProvider.account_number_role_list(spId, userId, targetUrl);
        log.info("角色下拉框 返回结果：{}", body);
        List<AccountNumberRoleListResp> resp = new ArrayList<>();
        if (null != body && null != body.getData()) {
            resp = JSONArray.parseArray(body.getData(), AccountNumberRoleListResp.class);
        }
        return excuteBody(body, resp);
    }

    @Override
    public CommonResponse<Boolean> checkMobile(String mobile) {
        String spId = ApplicationUtils.getWorkingSpId();
        String userId = ApplicationUtils.getUserId();
        CommonResponse<String> body = vccSystemProvider.account_number_check_mobile(spId, userId, mobile, targetUrl);
        log.info("校验手机号 返回结果：{}", body);
        if ( null != body && null != body.getData()) {
            AccountNumberCheckResp resp = JSONObject.parseObject(body.getData(), AccountNumberCheckResp.class);
            if (null != resp && null != resp.getCode() && resp.getCode() != 0) return CommonResponse.failure(resp.getCode(), resp.getMsg());
        }
        return excuteBody(body, true);
    }

    @Override
    public CommonResponse<Boolean> checkStaffCode(String staffCode) {
        String spId = ApplicationUtils.getWorkingSpId();
        String userId = ApplicationUtils.getUserId();
        CommonResponse<String> body = vccSystemProvider.account_number_check_staff_code(spId, userId, staffCode, targetUrl);
        log.info("校验员工工号 返回结果：{}", body);
//        {\"code\":1,\"msg\":\"当前员工工号已存在，不可重复创建\"}"
        if ( null != body && null != body.getData()) {
            AccountNumberCheckResp resp = JSONObject.parseObject(body.getData(), AccountNumberCheckResp.class);
            if (null != resp && null != resp.getCode() && resp.getCode() != 0) return CommonResponse.failure(resp.getCode(), resp.getMsg());
        }
        return excuteBody(body, true);
    }

    @Override
    public CommonResponse<Boolean> checkUserCode(String userCode) {
        String spId = ApplicationUtils.getWorkingSpId();
        String userId = ApplicationUtils.getUserId();
        CommonResponse<String> body = vccSystemProvider.account_number_check_user_code(spId, userId, userCode, targetUrl);
        log.info("校验账号 返回结果：{}", body);
        if (null != body && null != body.getData()) {
            List<AccountNumberResp> resp = JSONArray.parseArray(body.getData(), AccountNumberResp.class);
            if (CollectionUtils.isNotEmpty(resp)) return CommonResponse.failure(-1, "账号已存在！");
        }
        return excuteBody(body, true);
    }

    @Override
    public CommonResponse<AccountCheckPasswordResp> checkPassword(String password) {
        String spId = ApplicationUtils.getWorkingSpId();
        CommonResponse<String> body = vccSystemProvider.accountCheckPassword(spId, password, targetUrl);
        log.info("校验账号 返回结果：{}", body);
        if (null != body && null != body.getData()) {
            AccountCheckPasswordResp passwordResp = JSONArray.parseObject(body.getData(), AccountCheckPasswordResp.class);
            if (passwordResp != null) return excuteBody(body, passwordResp);
        }
        return CommonResponse.failure(-1, "密码不存在！");
    }

    @Override
    public CommonResponse<Object> genPassword() {
        String spId = ApplicationUtils.getWorkingSpId();
        CommonResponse<String> body = vccSystemProvider.accountGenPassword(spId, targetUrl);
        log.info("校验账号 返回结果：{}", body);
        if (null != body && null != body.getData()) {
            Object password = JSONArray.parse(body.getData());
            return excuteBody(body, password);
        }
        return CommonResponse.failure(-1, "密码生成失败！");
    }

    @Override
    public CommonResponse<Object> queryPwdConfig() {
        String spId = ApplicationUtils.getWorkingSpId();
        CommonResponse<String> body = vccSystemProvider.accountQueryPwdConfig(spId, targetUrl);
        log.info("查询密码规则：{}", body);
        if (null != body && null != body.getData()) {
            Object parse = JSONArray.parse(body.getData());
            return excuteBody(body, parse);
        }
        return CommonResponse.failure(-1, "查询密码规则失败！");
    }
}
